<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding:0px;background-color: white">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
			<form id="ff" method="post">
				<table id="basicInfo" cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="20%">单据编号</td>
						<td width="30%">
							<input style="width:100%" class="easyui-textbox" id="input_ORDER_NO" data-options="prompt:'由系统自动生成，不需填写',disabled:true"></input>
						</td>
						<td width="20%">
							<h id="star">* </h>库房名称</td>
						<td width="30%">
							<input class="easyui-combobox" style="width: 100%;" type="text" name="STOREHOUSE_NAME" id="input_STOREHOUSE_NAME" data-options="required:true" />
						</td >
					</tr>
					<tr>
						<td>
							<h id="star">* </h>库房负责人</td>
						<td>
							<input class="easyui-textbox" style="width:100%;" type="text" name="DUTYMAN" id="input_DUTYMAN" data-options="required:true,disabled:true" />
						</td>
						<td>
							<h id="star">* </h>维修时间</td>
						<td>
							<input class="easyui-datebox" style="width:100%;" type="text" name="REPAIR_DATE" id="input_REPAIR_DATE" data-options="required:true" />
						</td>
					</tr>
					<tr class="hiddentr">
						<td>ID</td>
						<td>
							<input style="width:100%" class="easyui-textbox" id="input_ID"></input>
						</td>
						<td>维修金额</td>
						<td>
							<input class="easyui-textbox" type="text" id="input_AMOUNT" name="AMOUNT" />
						</td>
						<td width="20%">
							<h id="star">* </h>库房编号</td>
						<td width="30%">
							<input class="easyui-textbox" style="width:100%;" type="text" name="STOREHOUSE_ID" id="input_STOREHOUSE_ID" data-options="prompt:'由系统自动生成，不需填写',disabled:true" />
						</td>
						
					</tr>
				</table>
				<table id="tt" style="margin: 0px auto;width: 100%;">

				</table>
				<div style="padding: 10px 0px;">
					<table>
						<tr>
							<th>维修金额合计</th>
							<td>
								<input type="text" style="border: none;" name="AMOUNT" />
							</td>
							<th>维修金额合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none;" id="dx" />
								<div id="dx"></div>
							</td>
						</tr>
					</table>
				</div>
				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 40%;height: 450px;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/finance.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
	<script type="text/javascript" src="../../../JS/FrozenShareFunction/FrozenShare.js"></script>
	<script type="text/javascript" src="../../../JS/MarketShareFunction/DialogShareFunction.js"></script>
	<script type="text/javascript" src="../../../JS/addRowShareFunction.js"></script>
	<script type="text/javascript">
		var currid = AddFlag;
		var id = getID();
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var isVerify = 0;
		var updateData;
		var _detailToolbarData;
		_detailToolbarData = AddDetailToolBar(7);
		$(document).ready(function(){
		if(id != AddFlag && id != undefined) {
			currid = id;
			loadRemote();
		}
    	});
		getPageMaxAndMin();
		addFooter($("#new_footer"));

		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);
		createShoreHouseCombobox();

		GetDetailTable();
		$('#tt').datagrid('hideColumn', 'ID');
		$('#tt').datagrid('hideColumn', 'ORDER_ID');

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/frozen/repairorder/getmaxminid.do";
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function newdata() { //新建
			currid = AddFlag;
			currPage = 0;
			resetData();
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
			$('#notreview').empty();
		}

		//导出
		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": currid
						},
						url: RemoteLinkAddress + "/frozen/repairorder/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
							//							this.onClick(location = result.path);
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		function frontpage() { //上一张
			if(currPage > MinPage) {
				var _url = RemoteLinkAddress + '/frozen/repairorder/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() { //下一张
			if(currPage < MaxPage) {
				var _url = RemoteLinkAddress + '/frozen/repairorder/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}
		}

		function save() { //保存
			var isValid = isTrueData();
			if(isValid != "验证成功") {
				$.messager.alert("警告", isValid);
				return;
			}
			if(isVerify === "1") {
				$.messager.alert("提示", "该单据已审核，不能修改！");
				return;
			}
			var s = $('#tt').edatagrid('getRows');
			if(s.length == 0) {
				$.messager.alert("警告", "明细不能为空");
				return;
			}
			if(currid != AddFlag) {
				getUpdateData();
			} else {
				getSaveData();
			}
		}

		function deletedata() { //删除
			if(isVerify === "1") {
				$.messager.alert("提示", "该单据已审核，不能删除！");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不可删除！");
				return;
			}
			$.messager.confirm('Confirm', '是否删除', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/frozen/repairorder/delete.do", {
							ID: currid
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({ // show success message
									title: '成功',
									msg: '删除成功'
								});
								nextpage();
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function verifydata() { //审核单据
			if(isVerify === "1") {
				$.messager.alert("警告", "已经审核无须再审");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不可审核！");
				return;
			}
			var _url = RemoteLinkAddress + "/frozen/repairorder/verify.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: {
					ID: currid
				},
				type: "POST",
				success: function(result) {
					$.messager.alert("成功", "审核成功");
					isVerify = "1";
					verifyStatus(isVerify, true);
				},
				error: function(result) {
					$.messager.alert("失败", "审核失败");
				}
			});
		}

		function unverifydata() { //弃审单据
			if(isVerify === "0") {
				$.messager.alert("警告", "已经弃审无须再弃审");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不可弃审！");
				return;
			}
			$.messager.confirm("警告", "你确认要弃审该条单据吗", function(r) {
				if(r) {
					var _url = RemoteLinkAddress + "/frozen/repairorder/unverify.do";
					$.ajax({
						dataType: 'JSON',
						url: _url,
						data: {
							ID: currid
						},
						type: "POST",
						success: function(result) {
							$.messager.alert("成功", "弃审成功");
							isVerify = "0";
							verifyStatus(isVerify, true);
						},
						error: function(result) {
							$.messager.alert("失败", "弃审失败");
						}
					});
				}
			});
		}

		function searchmore() { //查询
			AddTabIn("库房维修查询", parent.$('#tabs'), "UI/FrozenCave/StoreHouseRepair/StoreHouseRepairSearch.html");
		}

		function print() { //打印
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可打印！");
				return;
			}
			if(isVerify === "0") {
				$.messager.alert("警告", "未审核的单据不能打印！");
				return;
			}
			pLocation = "StoreHouseRepairPrint.html?id=" + currid;
			window.open(pLocation); /*打开pLocation中的链接html文档*/
		}

		function loadRemote() { //载入数据
			var _url = RemoteLinkAddress + '/frozen/repairorder/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						isVerify = result.ISVERIFY;
						verifyStatus(isVerify, true);
						currPage = result.ID;
						currid = result.ID;
						getFormData(result);
						updateData = deepCopy(result);
						var _data = result.rows;
						$('#tt').edatagrid('loadData', _data);
						CalSum();
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function getSaveData() {
			var data = submitFormData();
			data.rows = $('#tt').edatagrid('getRows');
			submitData(data);
		}

		function getUpdateData() {
			var data = submitFormData();
			for(s in data) {
				updateData[s] = data[s];
			}
			submitData(updateData);
		}

		function submitData(_data) {
			lockButton("lock");
			var _url = "";
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/frozen/repairorder/save.do";
			} else {
				_url = RemoteLinkAddress + "/frozen/repairorder/update.do";
			}
			var _senddata = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					lockButton("unlock");
					if(currid == AddFlag) {
						var revData = result.msg.split(";");
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						$.messager.alert("提示", "新建保存成功");
						currid = revData[1];
						currPage = revData[1];
						$(document).ready(function(){
						loadRemote();
						})
						return;
					}
					$.messager.alert("提示", "修改保存成功");
				},
				error: function(result) {
					$.messager.alert("错误信息", "保存失败");
				}
			});
		}

		function GetDetailTable() { //构造datagrid中的数据
			$('#tt').edatagrid({
				height: 300,
				toolbar: _detailToolbarData,
				collapsible: 'true', //是否折叠：是
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center'
					}, {
						field: 'ORDER_ID',
						title: '单据编号',
						width: '0%',
						align: 'center'
					}, {
						field: 'REPAIR_TYPE',
						title: '维护维修类型',
						width: '20%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								required: true,
								buttonAlign: 'right',
								buttonIcon: 'icon-add',
								valueField: "TYPE",
								textField: "TYPE",
								panelHeight: '150',
								mode: 'remote',
								method: 'get',
								url: RemoteLinkAddress + '/frozen/repairtype/listall.do',
								onClickButton: function() {
									AddRepairTypeName();
								}
							}
						}
					}, {
						field: 'CONTENT',
						title: '维修说明',
						width: '38%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "CONTENT",
								textField: "CONTENT",
							}
						}
					}, {
						field: 'REPAIR_MAN',
						title: '维修人员',
						width: '20%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "REPAIR_MAN",
								textField: "REPAIR_MAN",
							}
						}
					}, {
						field: 'PRICE',
						title: '维修金额',
						width: '18%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								valueField: "PRICE",
								textField: "PRICE",
							}
						},
						formatter: function(value, row, index) {
							if(value) {
								return '￥' + value + '元';
							}
						}
					}]
				],
				onEndEdit: function(index, row, changes) {
					CalSum();
					if(currid != AddFlag) {
						if(row.isNewRecord == false || row.isNewRecord == undefined) //即修改的情况
						{
							for(var i = 0; i < updateData.rows.length; i++) {
								if(updateData.rows[i].ID == row.ID) {
									var obj = updateData.rows[i];
									for(var s in obj) {
										if(obj[s] != row[s]) {
											obj[s] = row[s];
										}
									}
								}
							}
						} else {
							var len = updateData.rows.length;
							updateData.rows[len] = row;
							updateData.rows[len].ORDER_ID = currid;
						}
					}
				},
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				},
				onDestroy: function(index, row) {
					CalSum();
					if(currid != AddFlag) {
						for(var i = 0; i < updateData.rows.length; i++) {
							if(updateData.rows[i].ID == row.ID) {
								updateData.rows[i].ORDER_ID = ""; //直接替换
							}
						}
					}
				}
			})
		}

		function CalSum() {
			var _num = Number(0);
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				if(dataGridRows[i].CONTENT != undefined) {
					_num += Number(dataGridRows[i].PRICE);
				}
			}
			$("input[name='AMOUNT']").val(_num);
			$('#input_AMOUNT').textbox('setValue', _num);
			var num = changeMoneyToChinese(_num);
			$("#dx").val(num);
		}

		function AddRepairTypeName() {
			var pLocation = "UI/Market/DictData/DictDataAdd.html?id=repairtype";
			$("#ifrmEdit").attr("src", pLocation);
			constructcomboDialog($("#dlg"), "添加基础信息", $("#input_REPAIR_TYPE"));
			AddDialog.dialog('open');
			$('#tt').edatagrid('reload');
		}
	</script>

</html>